package reduce.fz.com.reduce.custom;

import android.annotation.TargetApi;
import android.app.Service;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.TypeReference;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;

import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.Follow;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.service.UserService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.base.ReduceApplication;

/**
 * Created by qingshengzheng on 15/9/7.
 */
public class FollowView  extends TextView{
    private UserService userService= ServiceBuilder.builderUserService(null);
    /**
     *  用户自己
     */
    private User me;
    /**
     * 别的用户
     */
    private User owner;
    /**
     * 自己的uid
     */
    private Long mid;
    /**
     * 别人的uid
     */
    private Long uid;
    /**
     * 他是否关注了你
     */
    private Boolean is_leader = false;
    /**
     * 你是否关注了他
     */
    private Boolean is_follower = false;

    private boolean withOutText = false;

    private boolean mAutoHidden = false;

    public void setFollowInterface(FollowInterface followInterface) {
        this.followInterface = followInterface;
    }

    private FollowInterface followInterface;

    public FollowView(Context context) {
        super(context);
    }

    public FollowView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doToggleAction();
            }
        });
    }

    public FollowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public FollowView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * 已经关注了用户以后是否自动消失
     * @param autoHidden
     */
    public void setAutoHidden(boolean autoHidden){
        mAutoHidden = autoHidden;
    }




    /**
     * 设置关注状态
     * @param mid   当前用户id
     * @param owner  别的用户
     */
    public void setStatus(Long mid, User owner) {
        setStatus(mid, owner, false);
    }

    public void setStatus(Long mid, User owner, boolean withOutText) {
        this.mid = mid;
        this.owner = owner;
        this.uid = owner.uid;
        this.is_follower = owner.userAttach.isfollow;
        this.is_leader = owner.userAttach.isleader;
        this.withOutText = withOutText;
        resetStatus(0);
    }


    /**
     * 重置按钮状态
     */
    public void resetStatus(boolean withOutText){
        this.withOutText = withOutText;
        resetStatus(0);
    }
    /**
     * 重置按钮状态
     * @param useAnimate 0 没有动画，1动画
     */
    public void resetStatus(int useAnimate){
        if(uid!=null && uid.equals(mid)){
            setVisibility(GONE);
            return;
        } else {
            setVisibility(VISIBLE);
        }
       if(owner.userAttach.isfollow ){//关注了他，他没关注我
            if(!withOutText) {
                setText(R.string.done_follow);
            }
            int x = getResources().getDimensionPixelSize(R.dimen.follow_view_padding1);
           int y = getResources().getDimensionPixelSize(R.dimen.follow_view_padding2);
            setPadding(x,y,x,y);
            setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimensionPixelSize(R.dimen.text_42));
            setSelected(true);
        } else { //有可能互相没关注，有可能他关注了我
            if(!withOutText) {
                setText(R.string.need_follow);
            }
            int x = getResources().getDimensionPixelSize(R.dimen.follow_view_padding);
           int y = getResources().getDimensionPixelSize(R.dimen.follow_view_padding2);
           setPadding(x,y,x,y);
            setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimensionPixelSize(R.dimen.text_42));
            setSelected(false);
        }
        //自动消失
        if(mAutoHidden && owner.userAttach.isfollow ){
            if( useAnimate == 0 ){
                setVisibility(GONE);
            } else {
                AlphaAnimation hideAnimation = new AlphaAnimation(1.0f, 0.0f);
                hideAnimation.setDuration(1000);
                hideAnimation.setFillAfter(true);
                hideAnimation.setStartOffset(400);
                startAnimation(hideAnimation);
            }
        } else {
            setVisibility(VISIBLE);
        }
    }

    @Override
    protected void onAnimationEnd() {
        setClickable(false);
        super.onAnimationEnd();
    }

    @Override
    protected void onAnimationStart() {
        setClickable(true);
        setVisibility(GONE);
        super.onAnimationStart();
    }

    public boolean doToggleAction(){
        User me = UserPreference.getUser();
        if(!owner.userAttach.isfollow){//发出关注请求
            if( me != null ){
                me.userAttach.follow_num++;
            }
            owner.userAttach.isfollow = true;
            resetStatus(1);
            if(followInterface!=null)
            {
                followInterface.onFollow(uid);
            }
           userService.follow(owner.uid, true, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>(){}) {
               @Override
               public void error(ServiceException e) {

               }

               @Override
               public void responseInfo(Result<HashMap> data) {

               }
           });
        } else { //发出取消关注请求
            if( me != null ){
                me.userAttach.follow_num--;
            }
            owner.userAttach.isfollow = false;
            resetStatus(1);
            userService.follow(owner.uid, false, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>(){}) {
                @Override
                public void error(ServiceException e) {

                }

                @Override
                public void responseInfo(Result<HashMap> data) {

                }
            });
            if(followInterface!=null) {
                followInterface.onDisFollow(uid);
            }
        }
        return true;
    }



    public  static interface FollowInterface{

        public void onFollow(Long uid);
        public void onDisFollow(Long uid);
    }

}
